/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.business.sys.controller.SysUserController.java 
 * <p>
 * 类产生时间: 2014-5-19 0019 下午 13:03
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package cn.com.oceansoft.business.sys.controller;

import cn.com.oceansoft.nos.common.constant.Constant;
import cn.com.oceansoft.nos.common.util.MD5Util;
import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.log.entity.SysBlackLog;
import cn.com.oceansoft.business.log.service.IBlackLogService;
import cn.com.oceansoft.business.sys.entity.SysRole;
import cn.com.oceansoft.business.sys.entity.SysUser;
import cn.com.oceansoft.business.sys.service.ISysRoleService;
import cn.com.oceansoft.business.sys.service.ISysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.*;

/**
 * 系统用户控制器
 *
 * @author: chenw
 * @time: 2014-5-19 0019 下午 13:03
 */
@Controller
@RequestMapping("sys/user")
public class SysUserController extends BaseController {

    @Resource
    private ISysUserService userService;

    @Resource
    private ISysRoleService roleService;

    @Resource
    private IBlackLogService blackLogService;

    /**
     * 进入系统用户管理首页
     *
     * @return String
     */
    @RequestMapping(value = "index", method = GET)
    public String gotoSysUserIndex(Query query, Model model) {
        query.setOrderField("ORDERINDEX");
        query.setOrderDirection("asc");
        model.addAttribute("query", query);
        model.addAttribute("pager", userService.findByPage(query));
        log("查看系统资源列表");
        return "sys/sys-user-list";
    }

    /**
     * 列表分页显示
     *
     * @param query 分页查询条件
     * @param model 数据Model
     * @return 页面地址
     */
    @RequestMapping(value = "list", method = POST)
    public String loadPageByCondition(Query query, Model model) {
        Pager<SysUser> pager = userService.findByPage(query);
        model.addAttribute("query", query);
        model.addAttribute("pager", pager);
        log(String.format("查看系统用户第%dÎ页", query.getPageNum()));
        return "sys/sys-user-list";
    }

    @RequestMapping(value = "view/{user_id}", method = GET)
    public String viewSysUser(@PathVariable("user_id") String userId, Model model) {
        if ("0".equals(userId)) {
            return "sys/sys-user-view";
        } else {
            SysUser user = userService.findUserById(userId);
            model.addAttribute("user", user);
            return "sys/sys-user-edit";
        }
    }

    /**
     * 新增系统用户
     *
     * @param sysUser SysUser
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "new", method = POST)
    public Result addSysUser(SysUser sysUser) {
        if (null != sysUser) {
            sysUser.setPassword(MD5Util.getMD5String(Constant.DEFAULT_USER_PASSWORD));
//            try {
////                String password = java.net.URLEncoder.encode(Constant.DEFAULT_USER_PASSWORD, "utf-8");
//                sysUser.setPassword(DesJLUtils.encrypt(password));
//            }catch (Exception e){
//                e.printStackTrace();
//                return new Result(Result.FAILURE, "密码加密失败");
//            }

            return userService.save(sysUser);
        }
        return new Result(Result.FAILURE, "提交参数有误");
    }

    /**
     * 新增系统用户
     *
     * @param sysUser SysUser
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "edit", method = POST)
    public Result editSysUser(SysUser sysUser) {
        if (null != sysUser) {
            return userService.update(sysUser);
        }
        return new Result(Result.FAILURE, "提交参数有误");
    }

    /**
     * 用户绑定角色信息
     *
     * @param userId 用户ID
     * @param model  Model
     * @return String
     */
    @RequestMapping("tree/role/{user_id}")
    public String bindUserRole(@PathVariable("user_id") String userId, Model model) {
        List<SysRole> roles = roleService.findRoleByUserId(userId);
        model.addAttribute("userId", userId);
        model.addAttribute("roles", roles);
        return "sys/user-role-bind";
    }

    /**
     * 新增用户角色关联信息
     *
     * @param userId 用户ID
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "bind/{user_id}", method = POST)
    public Result bindUserRole(@PathVariable("user_id") String userId, @RequestParam("ids") int[] ids) {
        userService.updateUserRoleBatch(userId, ids);
        return new Result();
    }

    /**
     * 新增系统用户
     *
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "del/{user_id}", method = DELETE)
    public Result delSysUser(@PathVariable("user_id") String userId) {
        return userService.delete(userId);
    }

    /**
     * 跳转到黑名单状态更新
     *
     * @param userId 用户id
     * @return
     */
    @RequestMapping(value = "change/{user_id}", method = GET)
    public String changeUser(@PathVariable("user_id") String userId, Model model) {
        model.addAttribute("user", userService.findUserById(userId));
        return "sys/sys-user-change";
    }

    @ResponseBody
    @RequestMapping(value = "save", method = POST)
    public Result saveUser(SysUser user, SysBlackLog log, HttpServletRequest request) {
        Result r = new Result(Result.FAILURE, "提交参数有误");
        //用户存在
        if (null != user) {
            String userId=request.getParameter("user.id");
            user.setId(userId);
            //更新用户信息
            r = userService.update(user);
            //获取登录人信息
            SysUser sess = getSession();
            //添加操作人id
            log.setOperate_id(sess.getId());
            //添加操作人姓名
            log.setOperate_name(sess.getRealName());
            //添加一条新的黑名单记录
            blackLogService.addBlackLogPage(log);
            return r;
        }
        return r;
    }
}
